<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('ACCESS', true);	

session_start();	

# set error checking levels #	
	# enable these lines when site is o be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
	#############################
	# include some needed files #
	#############################
		$absRoot = '../../';
		include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
		include($absRoot . 'src/ClientHandle.php');

	$id = $_GET["client_id"];

	$client = new ClientHandle();

	$ProfileArray=$client->getClientProfile($id);

	$ClientBirthday=$ProfileArray["client_birthday"];
	$BirthMonth=substr($ClientBirthday,5,2);
	$BirthDay=substr($ClientBirthday,8,2);
	$BirthYear=substr($ClientBirthday,0,4);
	
		 $january=""; 
		 $february=""; 
		 $march=""; 
		 $april=""; 
		 $may=""; 
		 $june=""; 
		 $july=""; 
		 $august=""; 
		 $september=""; 
		 $october=""; 
		 $november=""; 
		 $december=""; 		
	
	$selectedmonth=$BirthMonth;	 
		 
	switch ($BirthMonth)
	{
		case 1: $january="selected"; break;
		case 2: $february="selected"; break;
		case 3: $march="selected"; break;
		case 4: $april="selected"; break;
		case 5: $may="selected"; break;
		case 6: $june="selected"; break;
		case 7: $july="selected"; break;
		case 8: $august="selected"; break;
		case 9: $september="selected"; break;
		case 10: $october="selected"; break;
		case 11: $november="selected"; break;
		case 12: $december="selected"; break;		
	}

# instantiate predifined classes #											
	$xajax = new xajax();

	$xajax->registerFunction("datecheck");
	
	$xajax->registerFunction("editClientBirthdate");
	
	function datecheck($selectedmonth, $selectedday, $selectedyear) {
		$statusresponse = new xajaxResponse();
		
		if($selectedmonth==2)
		{
		if((($selectedyear % 4 == 0) && ($selectedyear % 100 != 0)) || ($selectedyear % 400 == 0))
		{
			$statusresponse->assign('29', 'disabled', '');
			$statusresponse->assign('30', 'disabled', 'true');
			$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>29)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('29', 'selected', 'true');
			}
		}
		else
		{
			$statusresponse->assign('29', 'disabled', 'true');
			$statusresponse->assign('30', 'disabled', 'true');
			$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>28)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('28', 'selected', 'true');
			}
		}
		
		}
		else if($selectedmonth==4 || $selectedmonth==6 || $selectedmonth==9 || $selectedmonth==11)
		{
		$statusresponse->assign('29', 'disabled', '');	
		$statusresponse->assign('30', 'disabled', '');
		$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>30)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('30', 'selected', 'true');
			}
		}
		else if($selectedmonth==1 || $selectedmonth==3 || $selectedmonth==5 || $selectedmonth==7 || $selectedmonth==8 || $selectedmonth==10 || $selectedmonth==12)
		{
		$statusresponse->assign('29', 'disabled', '');	
		$statusresponse->assign('30', 'disabled', '');
		$statusresponse->assign('31', 'disabled', '');
		}
					
		return$statusresponse;
	}
	
	function editClientBirthdate($BirthMonth,$BirthDay,$BirthYear,$id) {
		$statusresponse = new xajaxResponse();
		
		# check if the fields are empty
			if (empty($BirthMonth) || empty($BirthDay) || empty($BirthYear)) {
				$statusresponse->assign('Status','style.color','red');
				$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br /> Not updating..');
			}
			else {
			
			$client = new ClientHandle();

			$ProfileArray=$client->getClientProfile($id);

			$birthdate=$ProfileArray['client_birthday'];	
			
				$birthmonth=substr($birthdate,5,2);
				$birthday=substr($birthdate,8,2);
				$birthyear=substr($birthdate,0,4);
			
			$BirthDate=$BirthYear.'-'.$BirthMonth.'-'.$BirthDay;
				
					if ($BirthMonth == $birthmonth && $BirthDay == $birthday && $BirthYear == $birthyear) {
						$statusresponse->assign('Status','style.color','orange');
						$statusresponse->assign('Status', 'innerHTML', 'No changes is made.<br /> Not updating..');
					}
					elseif ($id != '') {
							
						$result = $client->setProfileValue('client_birthday', $BirthDate, $id);
						if($result == true)
						{
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()"); 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Birthdate changed.<br /> Database updated.');
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br />Not updating..');
						}
					}
			
			}
			
								
		return$statusresponse;
	}
	$xajax->processRequest();	

	//print_r($result);
?>

<html>
<head>
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>
</head>
<body onLoad="xajax_datecheck(document.getElementById('BirthMonth').value,document.getElementById('BirthDay').value,document.getElementById('BirthYear').value);">											


<form id="EditClientBirthdate" action="#" method='post'>
<table>
	
	<tr>
	<td>
	Month:</td>
	<td>
			<select id="BirthMonth" name="BirthMonth" onChange="xajax_datecheck(document.getElementById('BirthMonth').value,document.getElementById('BirthDay').value,document.getElementById('BirthYear').value);">
			<option value="1" <?php echo $january;?>>January</option>
			<option value="2" <?php echo $february;?>>February</option>
			<option value="3" <?php echo $march;?>>March</option>
			<option value="4" <?php echo $april;?>>April</option>
			<option value="5" <?php echo $may;?>>May</option>
			<option value="6" <?php echo $june;?>>June</option>
			<option value="7" <?php echo $july;?>>July</option>
			<option value="8" <?php echo $august;?>>August</option>
			<option value="9" <?php echo $september;?>>September</option>
			<option value="10" <?php echo $october;?>>October</option>
			<option value="11" <?php echo $november;?>>November</option>
			<option value="12" <?php echo $december;?>>December</option>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	Day:</td>
	<td>
			<select id="BirthDay" name="BirthDay">
			<?php $i=1;
			while($i < 32)
			{?>
			<option value="<?php echo $i;?>" id="<?php echo $i;?>" <?php if($BirthDay==$i) echo "selected";?>><?php echo $i;?></option>
			<?php $i++; } ?>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	Year:</td>
	<td>
			<select id="BirthYear" name="BirthYear" onChange="xajax_datecheck(document.getElementById('BirthMonth').value,document.getElementById('BirthDay').value,document.getElementById('BirthYear').value);">
			<?php $i=date("Y");
			while($i > 1919)
			{?>
			<option value="<?php echo $i;?>" id="<?php echo $i;?>" <?php if($BirthYear==$i) echo "selected";?>><?php echo $i;?></option>
			<?php $i--; } ?>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type=button id="Update" value="Update" onClick="xajax_editClientBirthdate(document.getElementById('BirthMonth').value,document.getElementById('BirthDay').value,document.getElementById('BirthYear').value, <?php echo $id;?>);"></input>
	<input type=reset id="Reset" value="Reset" onFocus="xajax_datecheck(document.getElementById('BirthMonth').value,document.getElementById('BirthDay').value,document.getElementById('BirthYear').value);"></input>
	</td>
	</tr>
	
	
</table>

</form>
<div id='Status'></div>

</body>

</html>
